/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Robin Chutaux
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.ml.carManager.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.content.ContextCompat;

import com.ml.carManager.R;

import custom.base.utils.TxtUtil;


/**
 * RippleView custom layout
 * <p/>
 * Custom Layout that allows to use Ripple UI pattern above API 21
 *
 * @author luozisong
 * @version 2016.0303
 */
public class BaseSelectLayout extends LinearLayoutCompat {
    private Paint paint;

    public BaseSelectLayout(Context context) {
        super(context);
    }

    public BaseSelectLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public BaseSelectLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private String name = "", content = "", hint = "";
    private boolean important, enAble = true, showIcon = true, bottomLine = false;
    private Integer paddingTop = 0, paddingBottom = 0;
    private Integer rightIcon = 0, contentWeight = 1, contentColor = 0;

    private View view;
    private TextView tvName, tvImportant;
    private TextView tvContent;
    private ImageView ivArrow;

    /**
     * Method that initializes all fields and sets listeners
     *
     * @param context Context used to create this view
     * @param attrs   Attribute used to initialize fields
     */
    private void init(final Context context, final AttributeSet attrs) {

        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BaseEditLayout);
        name = typedArray.getString(R.styleable.BaseEditLayout_edit_name);
        content = typedArray.getString(R.styleable.BaseEditLayout_edit_content);
        hint = typedArray.getString(R.styleable.BaseEditLayout_edit_hint);
        enAble = typedArray.getBoolean(R.styleable.BaseEditLayout_edit_enable, true);
        important = typedArray.getBoolean(R.styleable.BaseEditLayout_edit_important, false);
        showIcon = typedArray.getBoolean(R.styleable.BaseEditLayout_edit_icon_show, true);
        rightIcon = typedArray.getResourceId(R.styleable.BaseEditLayout_edit_icon, rightIcon);
        paddingTop = typedArray.getInteger(R.styleable.BaseEditLayout_edit_padding_top, paddingTop);
        paddingBottom = typedArray.getInteger(R.styleable.BaseEditLayout_edit_padding_bottom, paddingBottom);
        contentWeight = typedArray.getInteger(R.styleable.BaseEditLayout_edit_content_weight, contentWeight);
        bottomLine = typedArray.getBoolean(R.styleable.BaseEditLayout_edit_bottom_line, bottomLine);
        contentColor = typedArray.getResourceId(R.styleable.BaseEditLayout_edit_content_color, contentColor);
        typedArray.recycle();
        setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white));

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(params);
        setGravity(Gravity.CENTER_VERTICAL);
        setOrientation(HORIZONTAL);
        setPadding(dip2px(getContext(), 16), dip2px(getContext(), paddingTop), dip2px(getContext(), 16), dip2px(getContext(), paddingBottom));

        view = LayoutInflater.from(context).inflate(R.layout.view_base_item_layout, this);
        tvName = (TextView) view.findViewById(R.id.view_base_item_layout_name);
        tvImportant = (TextView) view.findViewById(R.id.view_base_item_layout_important);
        tvContent = (TextView) view.findViewById(R.id.view_base_item_layout_content);
        ivArrow = (ImageView) view.findViewById(R.id.view_base_item_layout_arrow);

        tvName.setText(name);
        if (!TxtUtil.isEmpty(content))
            tvContent.setText(content);
        if (!TxtUtil.isEmpty(hint))
            tvContent.setHint(hint);
        tvContent.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, contentWeight));

        if (important) {
            tvImportant.setVisibility(VISIBLE);
        } else {
            tvImportant.setVisibility(GONE);
        }

        this.setEnabled(enAble);
        if (enAble) {
            ivArrow.setVisibility(VISIBLE);
            tvName.setTextColor(ContextCompat.getColor(getContext(), R.color.app_txt_explain));
        } else {
            ivArrow.setVisibility(GONE);
            tvName.setTextColor(ContextCompat.getColor(getContext(), R.color.app_orange_auxiliary));//f颜色置灰
        }

        if (showIcon) {
            ivArrow.setVisibility(VISIBLE);
        } else {
            ivArrow.setVisibility(GONE);
        }
        if (rightIcon != null && rightIcon != 0) {
            ivArrow.setBackgroundResource(rightIcon);
        } else {
            ivArrow.setBackgroundResource(R.drawable.ic_arrow_right);
        }
        if (contentColor != null && contentColor != 0) {
            tvContent.setTextColor(ContextCompat.getColor(getContext(), contentColor));
        }

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(0xffe7e7e7);
    }

    public void setContent(String content) {
        this.content = content;
        tvContent.setText(content);
    }

    public void setText(String content) {
        setContent(content);
    }

    public String getText() {
        return tvContent.getText().toString();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (bottomLine)
            canvas.drawLine(dip2px(getContext(), 24), getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), paint);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public TextView getTvContent() {
        return tvContent;
    }

    public boolean isImportant() {
        return important;
    }
}
